// pages/my/my-index/my-index.js
const controllers=require('../../controllers/ControllerMy').controller
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype:'',
    myInfo:{},
    tabbar:[],
    head:'',
    nick:'',
    islogin:false,
    scnum:'',
    pcnum:'',
    ccnum:'',
    phone:'',
    serveState:{
      state2:"",
      state7:"",
      state9:"",
      state8:"",
      state1:"",
    },
    goodsState:{
      state11:"",
      state7:"",
      state8:"",
      state1:""
    },
    cloudState:{}
  },
  toCoupon(){
    wx.navigateTo({
      url: '/pages/my/vip/vip_coupon/index',
    })
  },
  toPoint(){
    wx.navigateTo({
      url: '/pages/my/vip/vip_grade/index',
    })
  },
  toAccount(){
    wx.navigateTo({
      url: '/pages/my/vip/vip_ surplus/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  //获取用户信息
  tologin(){
    if(wx.getStorageSync('usertoken')){

    }else{
      wx.navigateTo({
        url: '/pages/Login/Login',
      })
    }
  },
  //tabbar跳转
  tabChange(e){
    var index=e.detail.index;
    if(index==1){
      wx.requestSubscribeMessage({
        tmplIds:['CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0','IoewIlKNaUfOLOhKUOdPKMOnJxdtdqWGfOotuOB2R70','TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'],//'TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'
        success:res=>{
          console.log(res)
        },
        complete:()=>{
          wx.reLaunch({
            url: "/pages/Mall/Mall"
          });
        }
      })
    }
    if(index==2){
      wx.reLaunch({
        url: "/pages/cart/index/index"
      });
    }
    if(index==0){
     // wx.reLaunch({
      //   url: "/pages/Index/Index"
      // });

      wx.requestSubscribeMessage({
        tmplIds:['CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0','IoewIlKNaUfOLOhKUOdPKMOnJxdtdqWGfOotuOB2R70','TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'],//'TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'
        success:res=>{
          console.log(res)
        },
        complete:()=>{
          wx.reLaunch({
            url: "/pages/Index/Index"
          });
        }
      })
    }
  },
  //跳转到服务订单页面
  toService(e){
    wx.navigateTo({
      url: '/pages/my/service-order/service-order?id='+e.currentTarget.id,
    })
  },

  //跳转到商品订单页面
  toProduct(e){
    wx.navigateTo({
      url: '/pages/my/product-order/product-order?id='+e.currentTarget.id,
    })
  },

  //跳转到云商品订单页面
  toCloud(e){
    wx.navigateTo({
      url: '/pages/my/cloudproduct-order/cloudproduct-order?id='+e.currentTarget.id,
    })
  },
  //退出店铺
  quit(){
    wx.navigateTo({
      url: '/pages/Chain/Chain',
    })
  },

  //跳转到vip中心页面
  toVipCenter(){
    wx.navigateTo({
      url: '/pages/my/vip/index',
    })
  },
  onLoad: function (options) {
    this.setData({globalStype:app.globalData.globalStype})
    this.setData({
      tabbar: this.data.globalStype=='pinStyle'?app.globalData.tabbar:app.globalData.tabbar2,
    })
  },
  onShow: function () {
    if(wx.getStorageSync('phonenumber')){
      this.setData({
        phone:wx.getStorageSync('phonenumber')
      })
    }
    if (wx.getStorageSync('usertoken')) {
        //加载首页数据
        this.getMyInfo()
    }
    //无usertoken，等待login登录回调
    app.usertokenReadyCallback = res => {
      if(res.data.sessionId){
        this.getMyInfo()
      }
    }
  },
  getMyInfo(){
    //获取我的信息
    controllers.getMyInfo({
      shopId:app.globalData.shopId
    }).then(res =>{
      console.log(res);
      this.setData({
        myInfo:res.data
      })
      if(res.data){
        this.setData({
          islogin:true
        })
      }
      var that=this
      this.setData({
        'serveState.state2':'',
        'serveState.state7':'',
        'serveState.state9':'',
        'serveState.state8':'',
        'serveState.state1':'',
        'goodsState.state2':'',
        'goodsState.state9':'',
        'goodsState.state8':'',
        'goodsState.state1':'',
        'cloudState.state7':'',
        'cloudState.state11':'',
        'cloudState.state9':'',
        'cloudState.state8':'',
      })
      res.data.orderGoods.forEach(function(item,index){
        // if(item.goodsType==0){
        //   that.setData({
        //     scnum:item.num
        //   })
        // }else if(item.goodsType==1){
        //   that.setData({
        //     pcnum:item.num
        //   })
        // }else{
        //   that.setData({
        //     ccnum:item.num
        //   })
        // }
        if(item.goodsType==0){
          if(item.state==2){
            that.setData({
              'serveState.state2':item.num  //待预约
            })
          }else if(item.state==7){
            that.setData({
              'serveState.state7':item.num  //待服务
            })
          }else if(item.state==9){
            that.setData({
              'serveState.state9':item.num  //服务完成
            })
          }else if(item.state==8){
            that.setData({
              'serveState.state8':item.num  //待评价
            })
          }else if(item.state==1){
            that.setData({
              'serveState.state1':item.num  //拼团中
            })
          }

        }else if(item.goodsType==1){
          if(item.state==2){
            that.setData({
              'goodsState.state2':item.num  //待取货
            })
          }else if(item.state==9){
            that.setData({
              'goodsState.state9':item.num  //取货完成
            })
          }else if(item.state==8){
            that.setData({
              'goodsState.state8':item.num  //待评价
            })
          }else if(item.state==1){
            that.setData({
              'goodsState.state1':item.num  //拼团中
            })
          }

        }else{
          if(item.state==7){
            that.setData({
              'cloudState.state7':item.num  //通知取货
            })
          }else if(item.state==11){
            that.setData({
              'cloudState.state11':item.num  //待取货
            })
          }else if(item.state==9){
            that.setData({
              'cloudState.state9':item.num  //取货完成
            })
          }else if(item.state==8){
            that.setData({
              'cloudState.state8':item.num  //待评价
            })
          }

        }
      })
    })

    //获取用户信息
    if(wx.getStorageSync('wxuserInfo')){
      let userinfo=wx.getStorageSync('wxuserInfo')
      this.setData({
        head:userinfo.avatarUrl,
        nick:userinfo.nickName
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})